#!/usr/bin/env perl
######################################################################
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
#
#    Copyright (C) 2025 Network RADIUS SAS (legal@networkradius.com)
#
######################################################################
#
#  $Id: 3e20c613b5eccadd06f31d92169b604627f9835a $
#
#  ./scripts/upgrade/rewrite_attribute_names  -a attrs -i file -o file

use strict;
use warnings;
use Getopt::Std;
my $dictdir;
my $protocol;
my $dir;

my  %v3_to_v4;

our $opt_D = "./share/dictionary";
our $opt_i = "";
our $opt_o = "";

sub read_v4_alias {
    my $filename = shift;

    open(my $FILE, "<", $filename) or die "Failed to open $filename: $!\n";

    while (<$FILE>) {
	next if /^\s*#/;
	next if /^\s*$/;

	my @fields = split /\s+/;

	$v3_to_v4{$fields[0]} = $fields[1];
    }

    close $FILE;
}

#
#  Loop over the input file, breaking into words based on dictionary characters.
#
sub process_file {
    local *IN = shift;
    local *OUT = shift;


    while (<IN>) {
	my @array = split /([-A-Za-z0-9\/_]+)/;
	my $out;

	foreach my $word (@array) {
	    my $v4 = $v3_to_v4{$word};
	    if (defined $v4) {
		$out .= $v4;
	    } else {
		$out .= $word;
	    }
	}

	#
	#  Remove & from the start of the lines.
	#
	$out =~ s/^(\s*)&/$1/;

	#
	#  Remove & from attribute names
	#
	$out =~ s/([^&])&(3COM|3G|[a-zA-Z])/$1$2/g;

	#
	#  Change list names
	#
	$out =~ s/(request|reply|control|session-state):/$1./g;

	#
	#  Change function signatures
	#
	$out =~ s/%\{([a-zA-Z_0-9]+):/%$1\{/g;

	print OUT $out;
    }
}

#
#  Start of the program - get options
#
getopts('a:i:o:');

#
#  Read the alias mappings.
#
my @protocols = ( "freeradius", "radius" );
foreach (@protocols) {
    read_v4_alias($opt_D . "/" . $_ . "/v3/upgrade.txt");
}

#
#  Now that we have all of the attributes defined, let's process the input file.
#
#  @todo - allow input and output to be directories.  In which case we
#  recursively process the files in the input directory.
#
if ($opt_i ne "") {
    open INPUT, "<$opt_i"  or die "Couldn't open $opt_i: $!";
} else {
    open INPUT, "<&STDIN"  or die "Couldn't dup STDIN: $!";
}

if ($opt_o ne "") {
    open OUTPUT, ">$opt_o"  or die "Couldn't open $opt_o: $!";
} else {
    open OUTPUT, ">&STDOUT"  or die "Couldn't dup STDOUT: $!";
}

#
#  Process the data.
#
process_file(*INPUT, *OUTPUT);

close INPUT;
close OUTPUT;
